/**
 * @file   PropertiesUtils.java
 * @date   2012-11-12上午11:08:37
 * @author User
 */
package com.gzceb.zhgjj.util;


import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <p>属性配置文件加载类。</p>
 *
 * @date   2012-11-12上午11:08:37 
 * @author liqing
 * @description 
 */
public class PropertiesUtils {
	private static Log log = LogFactory.getLog(PropertiesUtils.class);
	private static Map<Object, Properties> propMap = new HashMap<Object, Properties>();
	private static final String DEFAULT_PROPERTIES = "config/system.properties";

	public PropertiesUtils(){
	}
	
	private static Properties getProperties(String propName){
		if (propName == null)
			return null;
		if (propMap.containsKey(propName))
			return (Properties)propMap.get(propName);
		Properties props = new Properties();
		try{
			ClassLoader loader = Thread.currentThread().getContextClassLoader();
			props.load(loader.getResourceAsStream(propName));
			propMap.put(propName, props);
		}
		catch (Exception e){
			log.error("获取Properties出错：", e);
		}
		return props;
	}
	
	public static String getValue(String key){
		Properties prop = getProperties(DEFAULT_PROPERTIES);
		if (prop != null)
			return prop.getProperty(key);
		else
			return null;
	}
	
	public static String getValue(String propName, String key){
		Properties prop = getProperties(propName);
		if (prop != null)
			return prop.getProperty(key);
		else
			return null;
	}
	
	public static String getValue(String propName, String key, String defaultValue){
		Properties prop = getProperties(propName);
		if (prop != null)
			return prop.getProperty(key, defaultValue);
		else
			return null;
	}
	
	public static int getIntValue(String key){
		String value = getValue(DEFAULT_PROPERTIES, key);
		int intValue = 0;
		if (value != null){
			try{
				intValue = Integer.parseInt(value.trim());
			}
			catch (NumberFormatException ex) {
				log.error("返回默认配置文件键值，内容为int类型出错：", ex); 
			}
		}
		return intValue;
	}
	
	public static int getIntValue(String propName,String key){
		String value = getValue(propName, key);
		int intValue = 0;
		if (value != null){
			try{
				intValue = Integer.parseInt(value.trim());
			}
			catch (NumberFormatException ex) {
				log.error("返回默认配置文件键值，内容为int类型出错：", ex); 
			}
		}
		return intValue;
	}
	
	public static long getLongValue(String key){
		String value = getValue(DEFAULT_PROPERTIES, key);
		long longValue = 0L;
		if (value != null){
			try{
				longValue = Long.parseLong(value.trim());
			}
			catch (NumberFormatException ex) {
				log.error("返回默认配置文件键值，内容为long类型出错：", ex); 
			}
		}
		return longValue;
	}
	
	public static long getLongValue(String propName,String key){
		String value = getValue(propName, key);
		long longValue = 0L;
		if (value != null){
			try{
				longValue = Long.parseLong(value.trim());
			}
			catch (NumberFormatException ex) {
				log.error("返回默认配置文件键值，内容为long类型出错：", ex); 
			}
		}
		return longValue;
	}
	
	public static boolean getBooleanValue(String key){
		String value = getValue(DEFAULT_PROPERTIES, key);
		boolean boolValue = true;
		if (value != null){
			try{
				boolValue = Boolean.parseBoolean(value.trim());
			}
			catch (NumberFormatException ex) {
				log.error("返回默认配置文件键值，内容为long类型出错：", ex); 
			}
		}
		return boolValue;
	}
	
	
}
